#pragma once

#include <QtGui/QApplication>
#include <QtGui/QMainWindow>
#include <QCoreApplication>
#include <QSystemTrayIcon>
#include <QMenu>
#include <QIcon>
#include <memory>
#include "windows.h"

#include "Worker.h"
#include "InputBox.h"
#include "CopyData.h"

class QSettings;


class MainModule : public QMainWindow
{
	Q_OBJECT

public:
	MainModule(QWidget* parent = 0, Qt::WFlags flags = 0);

public:
    void SetParent(HWND hwnd);
    void Execute();

	void ShowTrayIcon();
	void HideTrayIcon();

	void StartWorker();
	void StopWorker();

public slots:
    void OnSendMessage(const QString& message, int messageId, const QString& error);
    void NotifyWorker(const QByteArray& barCode);

private slots:
    void Exit();
    void IconActivated(QSystemTrayIcon::ActivationReason reason);
    void ShowReferralBox();

signals:
    void BarCode(const QByteArray&);

private:
	void CreateTrayIcon();
	void CreateMenuActions();
	void CreateMenu();

private:
    void Quit();

private:
    void WriteIni();
    void Initialize();

private:
    WorkerPtr   _worker;
    QString     _programName;
    HWND        _externalHWND;

private:
    typedef std::tr1::shared_ptr<QSystemTrayIcon> QSystemTrayIconPtr;
    typedef std::tr1::shared_ptr<InputBox> InputBoxPtr;
    typedef std::tr1::shared_ptr<QAction> QActionPtr;
    typedef std::tr1::shared_ptr<QMenu> QMenuPtr;

private:
    unsigned           _workerStopTimeout;
	QSystemTrayIconPtr _trayIcon;
    QMenuPtr		   _trayIconMenu;
	QActionPtr		   _labelAction;
    QActionPtr		   _quitAction;
    QActionPtr         _refAction;
    InputBoxPtr        _referralBox;
    QSettings          _ini;
};

typedef std::tr1::shared_ptr<MainModule> MainModulePtr;
